package it.polimi.TravelDream.ejb.entities;

import it.polimi.TravelDream.ejb.dtos.UserDTO;
import it.polimi.TravelDream.ejb.entities.Address;

import java.io.Serializable;
import java.lang.String;
import java.util.Collection;
import java.util.List;

import javax.persistence.*;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * Entity implementation class for Entity: User
 *
 */
@Entity
@Table(name = "users")
@NamedQueries({
	@NamedQuery(name=User.FIND_ALL, query="SELECT u FROM User u ORDER BY u.username ASC"),
	@NamedQuery(name=User.FIND_USER_BY_USERNAME, query = "Select u From User u where u.username = :username")
})

public class User implements Serializable {
	
	public static final String FIND_ALL = "User.findAll";
	public static final String FIND_USER_BY_USERNAME = "USer.FindUserByUsername";
	   
	@Id
	@Column(length=50, nullable=false)
	private String username;
	@Column(length=50, nullable=false)
	private String password;
	@Column(length=50, nullable=false)
	private String name;
	@Column(length=100, nullable=false)
	private String surname;
	@Column(length=50, nullable=false)
	private String email;

	//Chiave esterna tra USER e GROUP
	@ElementCollection(targetClass = Group.class)
    @CollectionTable(name = "USER_GROUP",
                    joinColumns = @JoinColumn(name = "users_username"))
    @Enumerated(EnumType.STRING)
    @Column(name="GROUPS_GROUPID")
    private List<Group> groups;

	//Indirizzo dell'utente
	@OneToOne
	@JoinColumn(name = "addresses_addressID", referencedColumnName = "addressID")
	private Address address;

	//Lista di pacchetti creati dall'impiegato
	@OneToMany(mappedBy = "employee")
	private Collection<BasicPackage> createdPackage;

	//Lista di pacchetti che l'utente ha PRENOTATO
	@ManyToMany(mappedBy = "users_who_booked")
	private List <UserPackage> userpackages_book;
	
	//Lista di pacchetti che l'utente ha PRENOTATO
	@ManyToMany(mappedBy = "users_who_bought")
	private List <UserPackage> userpackages_buy;

	private static final long serialVersionUID = 1L;

	public User() {
		super();
	}   
	
	public User(UserDTO user){
		this.username = user.getUsername();
		this.password = DigestUtils.sha256Hex(user.getPassword() );
		this.name = user.getName();
		this.surname = user.getSurname();
		this.email = user.getEmail();
	}
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}   
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}   
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}   
	public String getSurname() {
		return this.surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	} 
	
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<Group> getGroups() {
        return groups;
    }
 
    public void setGroups(List<Group> groups) {
        this.groups = groups;
    }  

	public Address getAddress() {
		return this.address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

}
